﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HidesaburoDotNET.Core;
using System.Drawing;
using System.Windows.Forms;

namespace HidesaburoMohjang
{
    public class TehaiDrawer 
    {

        Tehai tehai;
        Point position;

        List<PlaceHolder<Hai>> haiPlaceHolders = new List<PlaceHolder<Hai>>();
        List<PlaceHolder<Mentsu>> mentsuPlaceHolders = new List<PlaceHolder<Mentsu>>();

        public Tehai Tehai
        {
            get { return tehai; }
        }

        public Point Position
        {
            get { return position; }
            set { position = value; }
        }

        public TehaiDrawer(Tehai tehai):base()
        {
            this.tehai = tehai;
            
        }

        public void Draw(Graphics g){
            haiPlaceHolders.Clear();
            int curX = position.X, curY = position.Y;
            foreach (Hai h in tehai)
            {
                Image image = h.GetImage();
                g.DrawImage(image, curX, curY);
                haiPlaceHolders.Add(new PlaceHolder<Hai>(
                    h,
                    curX, curY, image.Width, image.Height));

                curX += image.Width;
            }
            if (tehai.LastDrew != null)
            {
                Image image = tehai.LastDrew.GetImage();
                curX += 10;
                g.DrawImage(image, curX, curY);
                haiPlaceHolders.Add(new PlaceHolder<Hai>(
                    tehai.LastDrew,
                    curX, curY, image.Width, image.Height));
                curX += image.Width;
            }
            curX += 20;
            //ないた面子を描画
            mentsuPlaceHolders.Clear();
            foreach (Mentsu m in tehai.NakiMentsuList)
            {
                Image[] images = m.GetMentsuImage();
                for (int i = 0; i < images.Length; i++)
                {
                    Image image = images[i];
                    g.DrawImage(image, curX, curY);
                    curX += image.Width;
                }
                curX += 5;
            }
        }

        /// <summary>
        /// 指定した位置にある牌を取得する
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public Hai GetIntersectedHai(Point point)
        {
            foreach (PlaceHolder<Hai> ph in haiPlaceHolders)
            {
                if (ph.Place.Contains(point))
                {
                    return ph.Target;
                }
            }
            return null;
        }
        public Hai GetIntersectedHai(int x, int y)
        {
            return GetIntersectedHai(new Point(x, y));
        }


    }
    class PlaceHolder<T>
    {
        Rectangle place;

        public Rectangle Place
        {
            get { return place; }
            set { place = value; }
        }
        T target;

        public T Target
        {
            get { return target; }
            set { target = value; }
        }

        public PlaceHolder(T target, Rectangle rect)
        {
            this.place = rect;
            this.target = target;
        }
        public PlaceHolder(T target, int x, int y, int w, int h)
        {
            this.place = new Rectangle(x, y, w, h);
            this.target = target;
        }

    }

    public class KawaDrawer
    {
        Kawa kawa;

        public Kawa Kawa
        {
            get { return kawa; }
        }
        Point position;

        public Point Position
        {
            get { return position; }
            set { position = value; }
        }


        public KawaDrawer(Kawa kawa)
        {
            this.kawa = kawa;
        }

        public void Draw(Graphics g)
        {
            int curX = position.X;
            int curY = position.Y;

            foreach (Hai h in kawa.InHou)
            {
                Image image = h.GetImage();
                g.DrawImage(image, curX, curY);
                curX += image.Width;
            }

        }
    }
}
